package com.take.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.take.common.CustomException;
import com.take.entity.Employee;
import com.take.mapper.EmployeeMapper;
import com.take.service.EmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService{

    @Override
    public void resetPasswordBatch(Long[] ids) {

        //检查ids中是否有1,1表示admin，密码不能被重置
        for (Long id : ids) {
            if(id==1){
                throw new CustomException("失败：用户中包含admin，无法重置admin的密码");
            }
        }

        //初始密码为123456
        String password123456 = DigestUtils.md5DigestAsHex("123456".getBytes());
        // ids中是员工们的id
        LambdaUpdateWrapper<Employee> luw = new LambdaUpdateWrapper<>();
        luw.in(Employee::getId,ids);
        luw.set(Employee::getPassword,password123456);
        update(luw);
    }

    @Override
    public void updatePasswd(Long empId, String newPwd) {
        LambdaUpdateWrapper<Employee> luw = new LambdaUpdateWrapper<>();
        luw.eq(Employee::getId,empId);
        luw.set(Employee::getPassword,newPwd);
        update(luw);
    }
}
